package com.fhz.day015;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    MediaPlayer mediaPlayer ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //播放视音频的
        mediaPlayer  = new MediaPlayer();
    }

    public void start(View view) {
        try {
            //重置
            mediaPlayer.reset();
            //设置数据源
            mediaPlayer.setDataSource("http://www.ytmp3.cn/down/57799.mp3");
            //都加载完成以后,才开始播放. 界面会卡.
//            mediaPlayer.prepare();
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                //准备完成
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void pause(View view) {
        mediaPlayer.pause();
    }

    public void stop(View view) {
        mediaPlayer.stop();
    }

    public void restart(View view) {
        mediaPlayer.start();
    }
}